package com.example.serialization.serializable.transfer.socket;

// Client.java

import com.example.serialization.serializable.transfer.Message;

import java.io.*;
import java.net.Socket;

public class Client {
    public static void main(String[] args) {
        final String SERVER_IP = "127.0.0.1";
        final int PORT = 9090;

        while (true) {
            try (Socket socket = new Socket(SERVER_IP, PORT);
                 ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
                 ObjectInputStream ois = new ObjectInputStream(socket.getInputStream())) {

                System.out.println("请输入要发送的消息：");
                String input = new BufferedReader(new InputStreamReader(System.in)).readLine();
                // 构造请求消息
                Message request = new Message("客户端", "服务端", input);

                // 发送消息
                oos.writeObject(request);
                oos.flush();
                System.out.println("[客户端发送] " + request);

                // 接收响应
                Message response = (Message) ois.readObject();
                System.out.println("[客户端接收] " + response);

            } catch (IOException | ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
    }
}